# -*- coding: utf-8 -*-
import oss2
from configparser import ConfigParser
import os

# 读取配置文件
home_path = os.path.dirname(os.path.realpath("~"))
print(home_path)
cfgfile = ConfigParser()
cfgfile.read(home_path + "/klipper_config/Klipper_AutoSave.cfg")

haveSaveConfig = cfgfile.has_section('Save-Config')

if haveSaveConfig == False:
    exit(0)

isOpen = (cfgfile.get("Save-Config", "isOpen"))
accessKeyId = (cfgfile.get("Save-Config", "AccessKeyId"))
accessKeySecret = (cfgfile.get("Save-Config", "AccessKeySecret"))
endPoint = (cfgfile.get("Save-Config", "EndPoint"))
bucketName = (cfgfile.get("Save-Config", "BucketName"))
savePath = (cfgfile.get("Save-Config", "SaveMkdirPath"))

if savePath == "/" :
    savePath = ""

result = cfgfile.has_section('FilePaths')
if result == False:
    exit(0)

options = cfgfile.options('FilePaths')

# 未开启功能 则退出程序
if isOpen == "0": 
    exit(0)

# 数组长度为0 则退出程序
if len(options) == 0:
    exit(0)

# 鉴权
# 阿里云账号AccessKey拥有所有API的访问权限，风险很高。强烈建议您创建并使用RAM用户进行API访问或日常运维，请登录RAM控制台创建RAM用户。
auth = oss2.Auth(accessKeyId, accessKeySecret)
bucket = oss2.Bucket(auth, endPoint, bucketName)

#上传文件
for filename in options:
    bucket.put_object_from_file(savePath + filename, (cfgfile.get('FilePaths', filename)))
